package com.hsmw.schedule.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.hsmw.common.base.BaseService;
import com.hsmw.domain.*;
import com.hsmw.schedule.mapper.HsmwReportMapper;
import com.hsmw.schedule.mapper.HsmwUserDriverMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@Slf4j
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwReportService extends BaseService<HsmwReportMapper, HsmwReport> {


    @Autowired
    private HsmwReportMapper hsmwReportMapper;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;



    boolean checkDriverHasleavingRecord(HashMap param) {
        Integer count = hsmwReportMapper.checkDriverHasleavingRecord(param);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }



    boolean checkIfHasleavingRecord(HashMap param) {
        String vehicleId = param.get("vehicleId").toString();
        AtomicInteger count = new AtomicInteger();
        List<HsmwUserDriver> drivers = hsmwUserDriverMapper.selectList(new LambdaQueryWrapper<HsmwUserDriver>().
                eq(HsmwUserDriver::getVehicleId, vehicleId));
        if (CollectionUtil.isNotEmpty(drivers)) {
            drivers.stream().forEach(hsmwUserDriver -> {
                param.put("driverId", hsmwUserDriver.getId());
                Integer res = hsmwReportMapper.checkDriverHasleavingRecord(param);
                if (res > 0) {
                    count.addAndGet(1);
                }
            });
            if (drivers.size() == count.intValue()) {
                return true;
            }
        }
        return false;
    }


}
